package com.huihua.it.camunda.service;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class CamundaService {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    // 1.部署xml文件
    public void deployXML(){
        String name = "请假流程";
        String xml = "process/diagram_1.bpmn";
        Deployment deploy = repositoryService
                .createDeployment()
                .name(name)
                .addClasspathResource(xml)
                .deploy();
        log.info("deploy is okar: {}",deploy.getId());
    }
    // 2.启动流程实例
    public void startProcess(String processInstanceId){
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceById(processInstanceId);
        log.info("start is okar:{}",processInstance.getId());
    }
    // 3.完成一个任务
    public void taskComplate(String taskId){
        taskService.complete(taskId);
        log.info("okar====");
    }

    // 3.完成一个任务加参数
    public void taskComplateVars(String taskId, Map map){
        taskService.complete(taskId,map);
        log.info("okar====");
    }
}
